﻿using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Business;

namespace ServicesDirectory.UserControl
{
    public partial class DetailDepartment : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            LookupBusinessType1.GetBusiness += LookupBusinessType1_GetBusiness;
            LookupBusinessType1.CloseControl += LookupBusinessType1_CloseControl;

            ListContact1.GetContactId += ListContact1_GetContactId;
            ListContact1.CloseControl += ListContact1_CloseControl;

            LookupAddress1.GetAddress += LookupAddress1_GetAddress;
            LookupAddress1.CloseControl += LookupAddress1_CloseControl;

            BtnInactive.Click += BtnInactive_Click;

            if (!IsPostBack)
            {
                if (Request.QueryString["active"] != null)
                {
                    var departmentId = new Guid(Request.QueryString["active"]);
                    var result = DepartmentBusiness.Instance.ChangeStatus(departmentId, true);
                    if (result)
                    {
                        Session["departmentid"] = departmentId.ToString();
                        Response.Redirect("DetailDepartment.aspx");
                    }
                }

                if (Session["departmentid"] != null)
                {
                    var departmentid = new Guid(Session["departmentid"].ToString());
                    var department = DepartmentBusiness.Instance.GetById(departmentid);
                    BindData(department);
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
            cmdBack.Attributes.Add("onClick", "javascript:history.back(); return false;");
            const string draggable = "$( '#draggable' ).draggable();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "draggable", draggable, true);
        }

        #region Lookup close control
        private void LookupAddress1_CloseControl(object sender, EventArgs e)
        {
            plLookupAddress.Visible = false;
        }

        private void ListContact1_CloseControl(object sender, EventArgs e)
        {
            plLookupContact.Visible = false;
        }

        private void LookupBusinessType1_CloseControl(object sender, EventArgs e)
        {
            plLookupBusiness.Visible = false;
        }
        #endregion

        #region Lookup get function
        private void LookupAddress1_GetAddress(object sender, EventArgs e)
        {
            var address = (Address)sender;
            var town = TownBusiness.Instance.GetTown(address.TownId.ToString());
            var county = CountyBusiness.Instance.GetCounty(address.CountyId.ToString());
            var country = CountryBusiness.Instance.GetCountry(address.CountryId.ToString());

            TxtPostcode.Text = address.PostCode;
            TxtTown.Text = town.TownName;
            TxtCounty.Text = county.CountyName;
            TxtCountry.Text = country.CountryName;
            hdfAddressId.Value = address.AddressId.ToString();
        }

        private void ListContact1_GetContactId(object sender, EventArgs e)
        {
            var contact = (Contact)sender;
            TxtLeadContact.Text = contact.FirstName + " " + contact.SurName;
            hdfContactId.Value = contact.ContactId.ToString();
        }

        private void LookupBusinessType1_GetBusiness(object sender, EventArgs e)
        {
            var business = (Entities.BusinessEntities)sender;
            TxtTypeBusiness.Text = business.BusinessName;
            TxtSicCode.Text = business.SicCode;
            hdfBusinessId.Value = business.BusinessId.ToString();
        }
        #endregion

        #region Lookup click
        protected void LbtLookupBusiness_Click(object sender, EventArgs e)
        {
            plLookupBusiness.Visible = true;
        }

        protected void LbtLookupContact_Click(object sender, EventArgs e)
        {
            plLookupContact.Visible = true;
        }

        protected void LbtLookupPostcode_Click(object sender, EventArgs e)
        {
            plLookupAddress.Visible = true;
        }
        #endregion

        #region Function
        protected void Save_Click(object sender, EventArgs e)
        {
            if (LblStatus.Text.Equals(String.Empty))
            {
                if (Session["departmentid"] != null)
                {
                    var departmentId = new Guid(Session["departmentid"].ToString());
                    var name = TxtName.Text;
                    var businessId = new Guid(hdfBusinessId.Value);
                    var contactId = new Guid(hdfContactId.Value);
                    var shortDesc = TxtShortDescription.Text;
                    var fullDesc = TxtFullDescription.Text;
                    var addressLine1 = TxtAddress1.Text;
                    var addressLine2 = TxtAddress2.Text;
                    var addressLine3 = TxtAddress3.Text;
                    var addressId = new Guid(hdfAddressId.Value);
                    var phoneNumber = TxtPhone.Text;
                    var fax = TxtFax.Text;
                    var email = TxtEmail.Text;
                    var webAddress = TxtWebAddress.Text;

                    var department = new Department
                    {
                        DepartmentId = departmentId,
                        Name = name,
                        BusinessId = businessId,
                        ContactId = contactId,
                        ShortDesc = shortDesc,
                        FullDesc = fullDesc,
                        AddressLine1 = addressLine1,
                        AddressLine2 = addressLine2,
                        AddressLine3 = addressLine3,
                        AddressId = addressId,
                        PhoneNumber = phoneNumber,
                        Fax = fax,
                        Email = email,
                        WebAddress = webAddress
                    };
                    var result = DepartmentBusiness.Instance.Update(department);
                    if (result)
                    {
                        const string script1 = "var result= window.confirm('Edit Department Successfully! Are you want to continue?'); " +
                                               "if(!result)" +
                                               " location.href=('DetailDirectorate.aspx');";

                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "updatesuccess", script1, true);
                    }
                }
            }
        }

        private void BindData(Department department)
        {
            TxtName.Text = department.Name;
            TxtShortDescription.Text = department.ShortDesc;
            var business = BusinessTypeBusiness.Instance.GetBusiness(department.BusinessId.ToString());
            TxtTypeBusiness.Text = business.BusinessName;
            TxtSicCode.Text = business.SicCode;
            TxtLeadContact.Text = department.LeadContact;
            TxtFullDescription.Text = department.FullDesc;
            TxtAddress1.Text = department.AddressLine1;
            TxtAddress2.Text = department.AddressLine2;
            TxtAddress3.Text = department.AddressLine3;
            TxtPhone.Text = department.PhoneNumber;
            TxtFax.Text = department.Fax;
            TxtEmail.Text = department.Email;
            TxtWebAddress.Text = department.WebAddress;

            if (department.AddressId != Guid.Empty)
            {
                var address = AddressBusiness.Instance.GetAddress(department.AddressId.ToString());
                var town = TownBusiness.Instance.GetTown(address.TownId.ToString());
                var county = CountyBusiness.Instance.GetCounty(address.CountyId.ToString());
                var country = CountryBusiness.Instance.GetCountry(address.CountryId.ToString());

                TxtPostcode.Text = address.PostCode;
                TxtTown.Text = town.TownName;
                TxtCounty.Text = county.CountyName;
                TxtCountry.Text = country.CountryName;
            }

            hdfAddressId.Value = department.AddressId.ToString();
            hdfBusinessId.Value = department.BusinessId.ToString();
            hdfContactId.Value = department.ContactId.ToString();
        }

        protected void CmdBackClick(object sender, EventArgs e)
        {

        }

        protected void BtnInactive_Click(object sender, EventArgs e)
        {
            var script = "var result= window.confirm('Do you want to make this Department in-active?'); " +
                         "if(result)" +
                         " location.href=('DetailDirectorate.aspx?inactive=" + Session["departmentid"] + "');";
            
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "markinactive", script, true);
        }

        protected void RblCopy_SelectedIndexChange(object sender, EventArgs e)
        {
            if (RblCopy.SelectedItem.Text.Equals(" Parent"))
            {
                if (Session["directorateid"] != null)
                {
                    var directorateid = new Guid(Session["directorateid"].ToString());
                    var directorate = DirectorateBusiness.Instance.GetById(directorateid);
                    TxtAddress1.Text = directorate.AddressLine1;
                    TxtAddress2.Text = directorate.AddressLine2;
                    TxtAddress3.Text = directorate.AddressLine3;

                    var address = AddressBusiness.Instance.GetAddress(directorate.AddressId.ToString());
                    var town = TownBusiness.Instance.GetTown(address.TownId.ToString());
                    var county = CountyBusiness.Instance.GetCounty(address.CountyId.ToString());
                    var country = CountryBusiness.Instance.GetCountry(address.CountryId.ToString());

                    TxtPostcode.Text = address.PostCode;
                    TxtTown.Text = town.TownName;
                    TxtCounty.Text = county.CountyName;
                    TxtCountry.Text = country.CountryName;
                    hdfAddressId.Value = address.AddressId.ToString();
                }
            }
            else
            {
                if (Session["orgId"] != null)
                {
                    var orgId = Session["orgId"].ToString();

                    var org = OrganizationBusiness.Instance.GetOrganisationById(orgId);
                    TxtAddress1.Text = org.AddressLine1;
                    TxtAddress2.Text = org.AddressLine2;
                    TxtAddress3.Text = org.AddressLine3;

                    var address = AddressBusiness.Instance.GetAddress(org.AddressId.ToString());
                    var town = TownBusiness.Instance.GetTown(address.TownId.ToString());
                    var county = CountyBusiness.Instance.GetCounty(address.CountyId.ToString());
                    var country = CountryBusiness.Instance.GetCountry(address.CountryId.ToString());

                    TxtPostcode.Text = address.PostCode;
                    TxtTown.Text = town.TownName;
                    TxtCounty.Text = county.CountyName;
                    TxtCountry.Text = country.CountryName;
                    hdfAddressId.Value = address.AddressId.ToString();
                }
            }
        }

        protected void TxtName_TextChanged(object sender, EventArgs e)
        {
            var departmentId = new Guid(Session["departmentid"].ToString());
            var name = TxtName.Text;
            var validate = DepartmentBusiness.Instance.ValidateName(departmentId, name);
            if (validate)
            {
                LblStatus.Text = String.Empty;
                TxtName.BorderStyle = BorderStyle.Solid;
                TxtName.BorderColor = Color.Green;
            }
            else
            {
                LblStatus.Text = Common.Constants.UIMessage.ValidateDepartmentNameFalse;
                LblStatus.ForeColor = Color.Red;
                TxtName.BorderStyle = BorderStyle.Solid;
                TxtName.BorderColor = Color.Red;
                TxtName.Focus();
            }
        }
        #endregion


    }
}